package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 商品属性
 *
 * @author 嘉岭月天
 * @email 嘉岭月天@cnnc.com
 * @date 2021-11-29 18:03:20
 */
@Api(tags = "商品属性 管理")
@RestController
@RequestMapping("pms/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    /**
     *根据以下三个参数查询分组规格参数
     * required=false 表示这个参数不是必须参数，前端可以不传，如果不加这个属性前端不传会报错
     * 商品管理/商品列表/查询sku的销售属性  http://api.gmall.com/pms/attr/category/225?type=0
     * @param cid
     * @param type
     * @param searchType
     * @return
     */
    @GetMapping("category/{cid}")
    public ResponseVo<List<AttrEntity>> queryAttrByCid(@ApiParam("分类id") @PathVariable("cid")Long cid,
                                                @ApiParam("是否基本属性") @RequestParam(value = "type",required = false) Integer type,
                                                 @ApiParam("是否搜索参数") @RequestParam(value = "searchType",required = false) Integer searchType){
        List<AttrEntity>attrEntities = this.attrService.queryAttrByCid(cid,type,searchType);
        return ResponseVo.ok(attrEntities);

    }

    /**
     * 根据分组id查询规格参数表（仅为参数表，不是参数值表  当type=1时查询的是基础表spu  =0时查询销售表sku）
     * @param gid
     * @return
     */
    @GetMapping("group/{gid}")
    public ResponseVo<List<AttrEntity>> queryAttrByGid(@PathVariable("gid")Long gid){
        List<AttrEntity> attrEntityList = this.attrService.list(new QueryWrapper<AttrEntity>().eq("group_id", gid));
        return ResponseVo.ok(attrEntityList);
    }

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = attrService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrEntity> queryAttrById(@PathVariable("id") Long id){
		AttrEntity attr = attrService.getById(id);

        return ResponseVo.ok(attr);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		attrService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
